package com.adcc.ags.gms.user.vo;

import com.adcc.ags.gms.core.vo.BaseVO;
import com.adcc.ags.gms.user.entity.Route;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;

import java.util.List;

/**
 * 路由视图
 */
public class RouteVO extends BaseVO {

    // ID
    private long id;

    // 名字
    private String name;

    // 类型(0:下行,1:上行,2:地地)
    private int type;

    // 发送地址
    private String sendAddress;

    // 接收地址
    private String recvAddress;

    // SMI
    private String smi;

    // 机尾号
    private String an;

    // 航班号
    private String fi;

    // RGS
    private String rgs;

    // 关键字起始位
    private int specIndex;

    // 关键字
    private String specLable;

    // 用户ID
    private long gwUserId;

    // 目的地址用户ID
    private List<String> destinationId = Lists.newArrayListWithExpectedSize(100);

    // 目的用户名称列表
    private List<String> destinationName = Lists.newArrayListWithExpectedSize(100);

    private GWUserVO gwUserVO;

    /**
     * 构造函数
     */
    public RouteVO(){

    }

    /**
     * 构造函数
     * @param route
     */
    public RouteVO(Route route){
        if(route != null){
            id = route.getId();
            name = route.getName();
            type = route.getType();
            sendAddress = route.getSendAddress();
            recvAddress = route.getRecvAddress();
            smi = route.getSmi();
            an = route.getAn();
            fi = route.getFi();
            rgs = route.getRgs();
            specIndex = (route.getSpecIndex() == null) ? 0 : route.getSpecIndex();
            specLable = route.getSpecLable();
            if(!Strings.isNullOrEmpty(route.getDestination())){
                String[] strDestArray = route.getDestination().split(",");
                for(String dest : strDestArray){
                    destinationId.add(dest);
                }
            }
            gwUserId=(route.getGwUserId() == null) ? 0 : route.getGwUserId();
            if(route.getGwUser() != null){
                gwUserVO = new GWUserVO(route.getGwUser());
            }
        }
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public String getSendAddress() {
        return sendAddress;
    }

    public void setSendAddress(String sendAddress) {
        this.sendAddress = sendAddress;
    }

    public String getRecvAddress() {
        return recvAddress;
    }

    public void setRecvAddress(String recvAddress) {
        this.recvAddress = recvAddress;
    }

    public String getSmi() {
        return smi;
    }

    public void setSmi(String smi) {
        this.smi = smi;
    }

    public String getAn() {
        return an;
    }

    public void setAn(String an) {
        this.an = an;
    }

    public String getFi() {
        return fi;
    }

    public void setFi(String fi) {
        this.fi = fi;
    }

    public String getRgs() {
        return rgs;
    }

    public void setRgs(String rgs) {
        this.rgs = rgs;
    }

    public int getSpecIndex() {
        return specIndex;
    }

    public void setSpecIndex(int specIndex) {
        this.specIndex = specIndex;
    }

    public String getSpecLable() {
        return specLable;
    }

    public void setSpecLable(String specLable) {
        this.specLable = specLable;
    }

    public long getGwUserId() {
        return gwUserId;
    }

    public void setGwUserId(long gwUserId) {
        this.gwUserId = gwUserId;
    }

    public List<String> getDestinationId() {
        return destinationId;
    }

    public void setDestinationId(List<String> destinationId) {
        this.destinationId = destinationId;
    }

    public List<String> getDestinationName() {
        return destinationName;
    }

    public void setDestinationName(List<String> destinationName) {
        this.destinationName = destinationName;
    }

    public GWUserVO getGwUserVO() {
        return gwUserVO;
    }

    public void setGwUserVO(GWUserVO gwUserVO) {
        this.gwUserVO = gwUserVO;
    }

    @Override
    public Route  toEntity() {
        Route route = new Route();
        route.setId(this.id);
        route.setName(this.name);
        route.setType(this.type);
        route.setSendAddress(this.sendAddress);
        route.setRecvAddress(this.recvAddress);
        route.setSmi(this.smi);
        route.setAn(this.an);
        route.setFi(this.fi);
        route.setRgs(this.rgs);
        route.setSpecIndex(this.specIndex);
        route.setSpecLable(this.specLable);
        route.setGwUserId(this.gwUserId);
        if(destinationId != null && destinationId.size() > 0){
            String strTemp = "";
            for(int i = 0;i < destinationId.size();i++){
                if(i != destinationId.size() - 1){
                    strTemp += destinationId.get(i) + ",";
                }else{
                    strTemp += destinationId.get(i);
                }
            }
            route.setDestination(strTemp);
        }else{
            route.setDestination("");
        }
        return route;
    }

    @Override
    public String toJson() throws JsonProcessingException {
        return new ObjectMapper().writeValueAsString(this);
    }
}
